﻿#ifndef PST_FarFieldDataGenerator_H
#define PST_FarFieldDataGenerator_H

#include <QObject>
#include <vtkSmartPointer.h>

class vtkTable;
class vtkDataSet;
class vtkStructuredGrid;

namespace pst
{
    class FarFieldDataGenerator : public QObject
    {
        Q_OBJECT

    public:
        FarFieldDataGenerator(QObject* parent = nullptr);
        ~FarFieldDataGenerator();

    public:
        void setInputData(vtkTable* table);

        void setLogTheData(bool useLog);
        bool getLogTheData();

        void setNormalizeTheData(bool useNormalize);
        bool getNormalizeTheData();

        void setTableExtends(int xMax, int yMax, int zmax);

        bool update();

        vtkDataSet* getOutputData(); // get the vtkDataSiz

        /**
         * 设置取对数时的因子，默认10
         * @para factory 因子
         */
        void SetLogFactory(double factory);
    private:
        /// @brief 从表格中提取theta参数(球坐标r,theta, phi)
        /// @param table 球坐标表格 (r,theta, phi)
        /// @param numVec 结果
        /// @return 是否成功
        bool ExtractPhiNum(vtkTable* table, std::vector<double>& numVec);

        /// @brief 将多行多列的球坐标表格转为四列的直角坐标表格
        /// @param inputTable 多行多列的球坐标表格
        /// @param outputTable 四列的直角坐标表格
        /// @return 是否成功
        bool transformRCSTableToFourColumnsRectangularTable(
            vtkTable* inputTable, vtkTable* outputTable, double corlorOffset);

        /// @brief 将表格转为StructuredGrid(表面重建)
        /// @param inputTable 输入的4列直角坐标表格
        /// @param outStructuredGrid 表面重建后的数据
        void transformTableToStructuredGrid(vtkTable* inputTable,
            vtkStructuredGrid* outStructuredGrid, int xMax, int yMax, int zMax);

        /// @brief 对表格取对数
        /// @param inputTable 输入表格(多行多列的球坐标表格)
        /// @param outputTable 输出表格(多行多列的球坐标取对数后的表格，只对r取对数)
        void logTheTable(vtkTable* inputTable, vtkTable* outputTable, double& logedMinimalValue);

        void normalizeTheTable(vtkTable* inputTable, vtkTable* outputTable);

    private:
        bool m_useLog;
        bool m_useNormalize;
        double m_logFactory;
        vtkTable* m_originalTable;
        vtkSmartPointer<vtkTable> m_resultTable;
        vtkSmartPointer<vtkStructuredGrid> m_resultDataSet;
        double m_tableExtends[3];
        int m_originalTableRowNumber;
        int m_originalTableColNumber;
    };
}
#endif // FarFieldDataGenerator
